<?php
	require('bindbank.doc.php');
	//绑定提现账号
	$app->post(
		'/bindbank/bind',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['paytype']) && isset($data['account']) && isset($data['captcha']) && isset($data['userid']) && isset($data['usertype'])){
				if($data['usertype'] == 1){
					$user = $db2->select(1, 'tb_member', '*', array('tbid' => $data['userid']));
					$captchatype = 14;
				}else{
					$user = $db2->select(1, 'tb_coach', '*', array('tbid' => $data['userid']));
					$captchatype = 24;
				}
				if($user){
					if(checkCaptcha($user['mobile'], $data['captcha'], $captchatype)){
						if(!$db2->select(1, 'tb_bind_bank', '*', array(
							'account' => $data['account'],
							'paytype' => $data['paytype'],
							'userid' => $data['userid'],
							'usertype' => $data['usertype']
						))){
							if(!$db2->select(1, 'tb_bind_bank', '*', array(
								'paytype' => $data['paytype'],
								'userid' => $data['userid'],
								'usertype' => $data['usertype']
							))){
								$set = array(
									'paytype' => $data['paytype'],
									'account' => $data['account'],
									'userid' => $data['userid'],
									'usertype' => $data['usertype'],
									'#dt' => 'now()'
								);
								if(isset($data['username'])){
									$set['username'] = $data['username'];
								}
								$db2->insert('tb_bind_bank', $set);
							}else{
								$db2->update('tb_bind_bank', array(
									'account' => $data['account'],
									'username' => $data['username'],
									'#dt' => 'now()'
								),array(
									'paytype' => $data['paytype'],
									'userid' => $data['userid'],
									'usertype' => $data['usertype']
								));
							}
						}else{
							$echo['error'] = "该账号已被绑定";
						}
					}else{
						$echo['error'] = "验证码错误";
					}
				}else{
					$echo['error'] = "用户不存在";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//解绑提现账号
	$app->post(
		'/bindbank/unbind',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['paytype']) && isset($data['userid']) && isset($data['usertype'])){
				if($data['usertype'] == 1){
					$user = $db2->select(1, 'tb_member', '*', array('tbid' => $data['userid']));
				}else{
					$user = $db2->select(1, 'tb_coach', '*', array('tbid' => $data['userid']));
				}
				if($user){
					$db2->delete('tb_bind_bank', array(
						'paytype' => $data['paytype'],
						'userid' => $data['userid'],
						'usertype' => $data['usertype']
					));
				}else{
					$echo['error'] = "用户不存在";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//获取当前绑定状态
	$app->get(
		'/bindbank/get',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['userid']) && isset($data['usertype'])){
				if($data['usertype'] == 1){
					$user = $db2->select(1, 'tb_member', '*', array('tbid' => $data['userid']));
				}else{
					$user = $db2->select(1, 'tb_coach', '*', array('tbid' => $data['userid']));
				}
				if($user){
					$bank = $db2->select(0, 'tb_bind_bank', '*', array(
						'userid' => $data['userid'],
						'usertype' => $data['usertype']
					));
					foreach($bank as $v){
						$tmp['paytype'] = $v['paytype'];
						$tmp['account'] = $v['account'];
						$tmp['username'] = $v['username'];
						$echo['data'][] = $tmp;
					}
				}else{
					$echo['error'] = "用户不存在";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
?>